home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pcmagazi / 1989 / 18 / argv.asm < prev    next >
Assembly Source File  |  1989-09-18  |  5KB  |  126 lines

  1.  
  2.         title   ARGV --- get address of command line argument
  3.         page    55,132
  4.         .386
  5.  
  6. ; ARGV.ASM:     Return address and length of specified 
  7. ;               command line argument or fully qualified 
  8. ;               program name.  Treats blanks and tabs 
  9. ;               as whitespace.
  10. ;
  11. ; 80386 version for Phar Lap DOS|Extender
  12. ; Copyright (C) 1989 Ziff Communications Co.
  13. ; PC Magazine * Ray Duncan
  14. ; Call with:    EAX    = argument number (0 based)
  15. ;
  16. ; Returns:      ES:EBX = argument address
  17. ;               EAX    = argument length (0=no argument)
  18. ;
  19. ; Uses:         nothing (other registers preserved)
  20. ;
  21. ; Note: if called with AX=0 (argv[0]), returns ES:EBX 
  22. ; pointing to fully qualified program name in environment 
  23. ; block and EAX=length.
  24. ;
  25. ; Warning: uses "magic" Phar Lap hard-wired selectors
  26. ; to address environment and PSP.  Code will need adjustment
  27. ; for other 80386 protected-mode environments.
  28.  
  29. cr      equ     0dh             ; ASCII carriage return
  30. tab     equ     09h             ; ASCII tab code
  31. blank   equ     20h             ; ASCII space code
  32.  
  33. pspsel  equ     24h             ; selector for prog seg prefix
  34. envsel  equ     2ch             ; selector for environment block
  35. cmdtail equ     80h             ; buffer for command tail
  36.  
  37. _TEXT   segment dword use32 public 'CODE'
  38.  
  39.         assume  cs:_TEXT
  40.  
  41.         public  argv
  42. argv    proc    near
  43.  
  44.         push    ecx             ; save registers
  45.         push    edi
  46.  
  47.         or      eax,eax         ; is it command tail argument 0?
  48.         jz      argv7           ; yes, jump to get program name
  49.  
  50.         mov     ebx,pspsel      ; set ES:EBX = command tail
  51.         mov     es,bx
  52.         mov     ebx,cmdtail
  53.  
  54.         xor     ah,ah           ; initialize argument counter
  55.  
  56. argv1:  mov     ecx,-1          ; set flag = outside argument
  57.  
  58. argv2:  inc     ebx             ; point to next character 
  59.         cmp     byte ptr es:[ebx],cr
  60.         je      argv6           ; exit if carriage return
  61.         cmp     byte ptr es:[ebx],blank
  62.         je      argv1           ; outside argument if ASCII blank
  63.         cmp     byte ptr es:[ebx],tab   
  64.         je      argv1           ; outside argument if ASCII tab
  65.  
  66.                                 ; not blank or tab...
  67.         jecxz   argv2           ; jump if already inside argument
  68.  
  69.         inc     ah              ; else count arguments found
  70.         cmp     ah,al           ; is this the one we're looking for?
  71.         je      argv3           ; yes, go find its length
  72.         not     ecx             ; no, set flag = inside argument
  73.         jmp     argv2           ; and look at next character
  74.  
  75. argv3:                          ; found desired argument, now
  76.                                 ; determine its length...
  77.         mov     eax,ebx         ; save param. starting address 
  78.  
  79. argv4:  inc     ebx             ; point to next character
  80.         cmp     byte ptr es:[ebx],cr
  81.         je      argv5           ; found end if carriage return
  82.         cmp     byte ptr es:[ebx],blank
  83.         je      argv5           ; found end if ASCII blank
  84.         cmp     byte ptr es:[ebx],tab   
  85.         jne     argv4           ; found end if ASCII tab
  86.  
  87. argv5:  xchg    ebx,eax         ; set ES:EBX = argument address
  88.         sub     eax,ebx         ; and EAX = argument length
  89.         jmp     argv9           ; return to caller
  90.  
  91. argv6:  xor     eax,eax         ; set EAX = 0, argument not found
  92.         jmp     argv9           ; return to caller
  93.  
  94. argv7:                          ; special handling for argv=0
  95.         mov     eax,envsel      ; get environment selector
  96.         mov     es,ax
  97.         xor     edi,edi         ; find the program name by
  98.         xor     al,al           ; first skipping over all the
  99.         mov     ecx,-1          ; environment variables...
  100.         cld
  101.  
  102. argv8:  repne scasb             ; scan for double null (can't use
  103.         scasb                   ; (SCASW since might be odd addr.)
  104.         jne     argv8           ; loop if it was a single null
  105.         add     edi,2           ; skip count word in environment
  106.         mov     ebx,edi         ; save program name address
  107.         mov     ecx,-1          ; now find its length... 
  108.         repne scasb             ; scan for another null byte
  109.         not     ecx             ; convert ECX to length 
  110.         dec     ecx
  111.         mov     eax,ecx         ; return length in EAX
  112.  
  113. argv9:                          ; common exit point
  114.         pop     edi             ; restore registers
  115.         pop     ecx
  116.         ret                     ; return to caller
  117.  
  118. argv    endp
  119.  
  120. _TEXT   ends
  121.  
  122.         end
  123.  
  124.  
  125.